package com.sz.sidi.oa.ui.login;

import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.nebula.widgets.pshelf.PShelf;
import org.eclipse.nebula.widgets.pshelf.PShelfItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.SWTResourceManager;

import util.DataSource;
import util.Session;

import com.sz.sidi.entity.TmenuMain;
import com.sz.sidi.oa.Activator;
import com.sz.sidi.oa.ui.enitor.EditorInput;
import com.sz.sidi.oa.ui.enitor.WelcomeEditor;


public class LeftView extends ViewPart {
	protected Logger logger = Logger.getLogger(getClass());
	public static final String ID = "login.LeftView"; //$NON-NLS-1$

	public LeftView() {
		setTitleImage(ResourceManager.getPluginImage("com.sz.sidi.oa", "icons/chart_16.png"));
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));

		PShelf shelf = new PShelf(parent, SWT.BORDER);
		shelf.setBackground(SWTResourceManager.getColor(SWT.COLOR_TITLE_BACKGROUND_GRADIENT));
		try{
		List<TmenuMain> menu = Session.loadSystemMenu(1,null);
		if(menu!=null && menu.size() > 0){
			for(TmenuMain id : menu){
				PShelfItem item = new PShelfItem(shelf, SWT.NONE);
				item.setImage(ResourceManager.getPluginImage("com.sz.sidi.oa", id.getFimage()));
				item.getBody().setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
				item.setText(id.getFmenuname());
				GridLayout gridLayout = new GridLayout(1, true);
				gridLayout.marginHeight = 0;
				gridLayout.verticalSpacing = 1;
				gridLayout.marginWidth = 1;
				gridLayout.horizontalSpacing = 1;
				item.getBody().setLayout(gridLayout);
				
				List<TmenuMain> menu_1 = Session.loadSystemMenu(2,id.getFmenuid());
				if(menu_1!=null && menu_1.size() > 0){
					for(final TmenuMain id_1 : menu_1){
						ToolBar toolBar = new ToolBar(item.getBody(), SWT.FLAT | SWT.RIGHT);
						toolBar.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
						toolBar.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
						
						final ToolItem toolItem = new ToolItem(toolBar, SWT.NONE);
						toolItem.setHotImage(null);
						toolItem.setImage(ResourceManager.getPluginImage("com.sz.sidi.oa", id_1.getFimage()));
						toolItem.setText(id_1.getFmenuname());	
						toolItem.addSelectionListener(new SelectionListener() {
							@Override
							public void widgetSelected(SelectionEvent e) {
								if(id_1.getFopenentiterid()!=null && !id_1.getFopenentiterid().equals("")){
								 createActions(id_1.getFopenentiterid());
								}else{
									logger.info("没有配置打开项目");
									return;
								}
							}
							@Override
							public void widgetDefaultSelected(SelectionEvent e) {
							}
						});
					}
				}
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		init();
	}

	/**
	 * Create the actions.
	 */
	private void createActions(String openEnitor) {
		try {
			IEditorInput input = new EditorInput(openEnitor);
			IWorkbenchPage page = Activator.getDefault().getWorkbench()
					.getActiveWorkbenchWindow().getActivePage();
			page.openEditor(input, openEnitor);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//系统进来默认打开一个Enitor
	public void init() {
		try {
			IEditorInput input = new EditorInput(WelcomeEditor.ID);
			IWorkbenchPage page = Activator.getDefault().getWorkbench()
					.getActiveWorkbenchWindow().getActivePage();
			IEditorPart editor = page.findEditor(input);
			if (null != editor) {
				page.bringToTop(editor);
			} else {
				page.openEditor(input, WelcomeEditor.ID);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setFocus() {
		
	}
}
